package com.sbs.gaoxinqutoilet.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.sbs.gaoxinqutoilet.myapp.MyApplication;

import java.util.Map;
import java.util.Set;


public class SPUtil {
    private static SharedPreferences mUserPreferences;
    private static final String USER_PREFERENCE = "user_preference";

    public static SharedPreferences ensureIntializePreference() {
        if (mUserPreferences == null) {
            MyApplication applicationIntstance = MyApplication.getApplicationIntstance();
            mUserPreferences = applicationIntstance.getSharedPreferences(USER_PREFERENCE, Context.MODE_APPEND);
        }
        return mUserPreferences;
    }

    public static void putString(String key, String value) {
        Editor editor = ensureIntializePreference().edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static String getString(String key, String defaultvalue) {
        return ensureIntializePreference().getString(key, defaultvalue);
    }

    /**
     *  写入一个 集合
     * @param key
     * @param value
     */
    public static void putStringSet(String key, Set<String> value){
        Editor edit = ensureIntializePreference().edit();
        edit.putStringSet(key,value);
        edit.commit();
    }
    public static Set<String> getStringSet(String key, Set<String> defaultvalue){
        return ensureIntializePreference().getStringSet(key,defaultvalue);
    }

    /**
     * SP中写入boolean
     *
     * @param key 键
     * @return 存在返回对应值，不存在返回默认值{@code false}
     */
    public static void setBoolean(String key, boolean value) {
        ensureIntializePreference().edit().putBoolean(key, value).commit();
    }

    /**
     * SP中读取boolean
     *
     * @param key          键
     * @param defaultValue 默认值
     * @return 存在返回对应值，不存在返回默认值{@code defaultValue}
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        return ensureIntializePreference().getBoolean(key, defaultValue);
    }

    /**
     * SP中写入int类型value
     *
     * @param key   键
     * @param value 值
     */
    public static void putInt(String key, int value) {
        ensureIntializePreference().edit().putInt(key, value).commit();
    }

    /**
     * SP中读取int
     *
     * @param key          键
     * @param defaultValue 默认值
     * @return 存在返回对应值，不存在返回默认值{@code defaultValue}
     */
    public static int getInt(String key, int defaultValue) {
        return ensureIntializePreference().getInt(key, defaultValue);
    }

    public static void putBeanValue(String key, Object object) {


        String objectValue = GsonTools.createGsonString(object);
        Log.e("显示",mUserPreferences+"  "+objectValue);

        ensureIntializePreference().edit().putString(key, objectValue).commit();
    }

    public static <T> T getBeanValue(String key, Class<T> cls) {

        String stringBean = ensureIntializePreference().getString(key, null);

        return GsonTools.changeGsonToBean(stringBean, cls);
    }


    /**
     * SP中获取所有键值对
     *
     * @return Map对象
     */
    public static Map<String, ?> getAll() {
        return ensureIntializePreference().getAll();
    }

    /**
     * SP中移除该key
     *
     * @param key 键
     */
    public static void remove(String key) {
        ensureIntializePreference().edit().remove(key).commit();
    }

    /**
     * SP中是否存在该key
     *
     * @param key 键
     * @return {@code true}: 存在<br>{@code false}: 不存在
     */
    public static boolean contains(String key) {
        return ensureIntializePreference().contains(key);
    }

    /**
     * SP中清除所有数据
     */
    public static void clear() {
        ensureIntializePreference().edit().clear().commit();
    }

}
